package com.ttc.group2.videochat.controller;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.internal.view.menu.MenuBuilder;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.quickblox.core.QBEntityCallback;
import com.quickblox.users.QBUsers;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.database.DbHelper;
import com.ttc.group2.videochat.model.User;
import com.ttc.group2.videochat.utils.DialogUtils;

public class OverflowPeopleSelectedListener implements OnClickListener {
	private Context mContext;
	private User qbUser;

	public OverflowPeopleSelectedListener(Context context, User qbUser) {
		this.mContext = context;
		this.qbUser = qbUser;
	}

	@Override
	public void onClick(View v) {
		PopupMenu popupMenu = new PopupMenu(mContext, v) {

			@Override
			public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
				switch (item.getItemId()) {
				case R.id.overflow_remove:
					// remove people
					removeUser();
					return true;
				case R.id.overflow_view_profile:
					// view profile
					// show view profile activity
					return true;

				default:
					return super.onMenuItemSelected(menu, item);
				}

			}

		};

		popupMenu.inflate(R.menu.overflow_menu_item);
		// add or remove item by code here
		popupMenu.show();
	}

	private void removeUser() {
		AlertDialog dialog = (AlertDialog) DialogUtils.createDialog(mContext, R.string.confirm_remove_user,
				R.string.ask_remove_user, positiveListener, negativeListener);
		dialog.show();
	}	

	private DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			DbHelper.getInstance(mContext).deleteUser(qbUser.getUserId());
		}
	};

	private DialogInterface.OnClickListener negativeListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.cancel();
		}
	};
}
